package com.wisdom.smartbook.facelib.engine;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.Random;

public class ActionEngine {
    private static ActionEngine actionEngine;
    public static ActionEngine getInstance(){
        if(actionEngine==null){
            synchronized (ActionEngine.class){
                actionEngine = new ActionEngine();
            }
        }
        return actionEngine;
    }

    private static int shouldDoAction ;
    private static long lastTime;
    public void start(){
        shouldDoAction = new Random().nextInt(2);// 0 - 1
        lastTime = 15000;
        if(thread==null){
            thread = new Thread(runnable);
            thread.start();
        }
        Message message = new Message();
        message.what=2;
        message.arg1 = shouldDoAction ;
        handler.sendMessage(message);
    }


    private Runnable runnable = () -> {
        checkHasOverTime();
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    private Thread thread ;

    public void destory(){
        if(thread!=null){
            thread.stop();
            thread = null;
        }
    }
    /**
     * 检测是否有超时
     */
    private void checkHasOverTime(){
        if(!canRunTime||lastTime<0){
            return;
        }
        lastTime-=30;
        if(lastTime<0){
            Message message = new Message();
            message.what=1;
            message.arg1 = 0 ;
            handler.sendMessage(message);
        }
    }

    private boolean canRunTime = false;
    public void onResult(int action){
        Log.e("onResult->action",shouldDoAction+"/"+action);
        if(action<=4) {
            canRunTime = true;
            Message message = new Message();
            if (action == shouldDoAction) {
                message.what = 1;
                message.arg1 = 1;
            } else {
                message.what = 2;
                message.arg1 = shouldDoAction;
            }
            handler.sendMessage(message);
        }else{
            canRunTime = false;
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    int arg1 = msg.arg1;
                    if(onActionCallback!=null){
                        boolean isOk = arg1==1;
                        onActionCallback.onResult(isOk,isOk?"认证成功":"认证失败");
                    }
                    break;
                case 2:
                    int arg = msg.arg1;
                    if(onActionCallback!=null){
                        String m = arg==0?"请眨眨眼":"请张张嘴";
                        onActionCallback.onMessage(m);
                    }
                    break;
            }
        }
    };

    private OnActionCallback onActionCallback;

    public OnActionCallback getOnActionCallback() {
        return onActionCallback;
    }

    public void setOnActionCallback(OnActionCallback onActionCallback) {
        this.onActionCallback = onActionCallback;
    }

    public interface OnActionCallback{
        void onMessage(String msg);
        void onResult(boolean isOk,String msg);
    }
}
